﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Commerce.Proxy.TransactionService.v201001;
using System.Xml;
using System.Collections;

namespace TestWebApplication
{
    public class ItemHandler
    {
        public static Hashtable allItems = new Hashtable();

        public static bool isSingleItemsLoaded = false;

        public static bool isBundleLoaded = false;

        public static XmlElement loadFile()
        {
            XmlDocument xml = new XmlDocument();
            String fileLocation = HttpContext.Current.Server.MapPath(Constants.ItemFileLocation);
            fileLocation = fileLocation.Remove(fileLocation.IndexOf(ServiceSettings.Api), ServiceSettings.Api.Length+1);
            xml.Load(fileLocation);
            
            XmlElement root = xml.DocumentElement;

            return root;
        }

        public static List<PurchaseContentProductItemInput> loadAllSingleItems()
        {

            XmlElement root = loadFile();

            XmlNodeList nodes = root.SelectNodes("descendant::SingleItem");

            List<PurchaseContentProductItemInput> items = new List<PurchaseContentProductItemInput>();
            foreach (XmlNode node in nodes)
            {
                PurchaseContentProductItemInput item = new PurchaseContentProductItemInput();
                item.Title = loadProperty(node, "Title");
                item.Description = loadProperty(node, "Description");
                item.RevenueInfo = new RevenueInfo();
                item.RevenueInfo.RevenueSKU = loadProperty(node, "RevenueSKU"); ;
                item.RevenueInfo.RevenueAllocationPriceType = RevenueAllocationPriceType.Amount;
                item.RevenueInfo.RevenueAllocationAmount = 7.0m;
                item.RevenueInfo.FinancialReportingAllocationPriceType = FinancialReportingAllocationPriceType.Amount;
                item.ExternalProductItemId = Guid.NewGuid().ToString();
                item.ExternalProductItemCatalogId = Guid.NewGuid().ToString();
                item.ProductType = loadProperty(node, "ProductType");

                items.Add(item);
                string id = loadProperty(node, "ID");
                if (!allItems.ContainsKey(id)) allItems.Add(id, item);
                

            }

            
            return items;
        }

        public static List<PurchaseBundleInput> loadAllBundles()
        {
            
            XmlElement root = loadFile();

            XmlNodeList nodes = root.SelectNodes("descendant::Bundle");

            List<PurchaseBundleInput> bundles = new List<PurchaseBundleInput>();
            foreach (XmlNode node in nodes)
            {
                PurchaseBundleInput bundle = new PurchaseBundleInput();
                bundle.Title = loadProperty(node, "Title");
                bundle.Description = loadProperty(node, "Description");
                bundle.Currency = loadProperty(node, "Currency");
                bundle.Amount = 15.98m;
                
                XmlNodeList itemNodes = node.SelectNodes("descendant::Item");
                List<PurchaseContentProductItemInput> items = new List<PurchaseContentProductItemInput>();
                foreach (XmlNode itemNode in itemNodes)
                {
                    object itemo = allItems[itemNode.InnerText];
                    PurchaseContentProductItemInput item = (PurchaseContentProductItemInput)itemo;
                    items.Add(item);
                }

                bundle.PurchaseProductItemInputSet = items.ToArray();
                string id = loadProperty(node, "ID");
                if (!allItems.ContainsKey(id)) allItems.Add(id, bundle);
                bundles.Add(bundle);
            }
            return bundles;
        }

        public static string loadProperty(XmlNode node, string propertyName)
        {
            return node.SelectSingleNode("descendant::" + propertyName).InnerText;
        }

        public static object getItem(string id)
        {
            return allItems[id];
        }
    }
}